package MULE.Model;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JButton;

import MULE.Model.Logic.MuleDriver;

/**
 * Represents a map tile. This class defines data and behavior common to all map
 * tiles.
 * 
 * @ author Anastasiya Cotton @ version 1.0 10.04.2013 @ author Aaron Englebardt @
 * version 1.1 10.16.2013
 */
public abstract class Tile extends JButton {

	// Tile dimension constants.
	public static final int WIDTH = MuleDriver.WINDOW_WIDTH / Map.COLUMNS;
	public static final int HEIGHT = WIDTH;
	public static final int BORDER_WIDTH = 2;
	// Tile color constants.
	public static final Color BORDER_COLOR = new Color(212, 212, 212);

	protected int x;
	protected int y;
	protected Color borderColor;
	protected Color backgroundColor;
	protected Player owner;

	/**
	 * Creates a new Tile at the specified coordinates.
	 * 
	 * @param x
	 *            X-coordinate of the tile.
	 * @param y
	 *            Y-coordinate of the tile.
	 */
	public Tile(int x, int y) {
		this.x = x;
		this.y = y;
		this.setPreferredSize(new Dimension(WIDTH, HEIGHT));
		this.setFocusable(false);
		this.backgroundColor = Color.BLACK;
		this.borderColor = BORDER_COLOR;
	}

	/**
	 * Returns the owner of this tile.
	 * 
	 * @return Player who owns this tile.
	 */
	public Player getOwner() {
		return owner;
	}

	/**
	 * Returns whether or not the Tile can be purchased.
	 * 
	 * @return True if the tile can be purchased, false otherwise.
	 */
	abstract boolean isPurchasable();

	abstract Inventory getProduction();

	/**
	 * Sets the tile's border color.
	 * 
	 * @param color
	 *            The tile's new border color.
	 */
	public void setBorderColor(Color color) {
		this.borderColor = color;
	}

	/**
	 * Draws the tile on the given graphics object.
	 * 
	 * @param g
	 *            The canvas on which to draw the tile.
	 */
	public void paintComponent(Graphics g) {
		// Downcast graphics object.
		Graphics2D g2 = (Graphics2D) g;
		// Draw background.
		g2.setColor(this.backgroundColor);
		g2.fillRect(0, 0, WIDTH, HEIGHT);
		// Draw border.
		g2.setColor(this.borderColor);
		g2.setStroke(new BasicStroke(BORDER_WIDTH));
		int borderOffset = BORDER_WIDTH / 2;
		g2.drawRect(borderOffset, borderOffset, WIDTH - BORDER_WIDTH, HEIGHT
				- BORDER_WIDTH);
		g2.dispose();
	}
}
